﻿@model NetSurveillance.WebApp.Models.HostScanModel
@using Resources
@{
    ViewBag.Title = "主机扫描";
    Layout = "~/Views/Shared/_SimpleLayout.cshtml";
}
@section css{
    <style type="text/css">
        .editor-label
        {
            float: left;
            margin-left: 25px;
        }

        label
        {
            margin-bottom: 15px;
        }

        #divScan input
        {
            height: 25px;
        }

        #divInfo input
        {
            margin-right: 15px;
            margin-top: 5px;
            width: 60px;
        }

        #topDiv
        {
            margin-top: 5px;
            float: left;
            border: solid 1px lightblue;
            height: 15px;
            width: 100%;
            font-style: normal;
            color: forestgreen;
        }

        .divMain
        {
            height: 450px;
            overflow: auto;
            overflow-x: hidden;
            float: left;
        }

        #divMainList
        {
            height: 420px;
            border: 1px solid #BABABA;
            overflow: auto;
            overflow-x: hidden;
            float: left;
        }
    </style>
}
@section javascript
{
    <script type="text/javascript">
        var isScan = false;
        $(function() {
            $('#hidStartIp').placeholder();
            $('#hidEndIp').placeholder();
            $("#btnScan").bind("click", validateIp);
            $("#btnStop").bind("click", function() {
                stopScan();
                isScan = false;
            });
            $("#btnExit").bind("click", exitConfirm);
            $(window).bind('beforeunload', function () {
                if (isScan) {
                    stopScan();
                    return "扫描正在进行中，是否退出？";
                }
            });
            /*$(window).unload(function () {
                if (isScan) {
                    setTimeout(stopScan, 0);
                }
            });*/
        });

        function validateIp() {
            var startIp = $("#hidStartIp").val();
            var endIp = $("#hidEndIp").val();

            if ($('form').valid()) {
                if (startIp == endIp) {
                    alert("开始IP地址不能和结束IP地址相同");
                    return;
                }
                $(this).attr("disabled", "disabled");
                $("#btnStop").removeAttr("disabled");
                $(this).val("扫描中...");
                $("#table_HostScanResults").html("");
                beginScan();
                return;
            }
            alert('输入的IP地址有问题，请将标红处改正后再试');
        }

        function exitConfirm() {
            if (isScan) {
                if (confirm("扫描正在进行中，是否退出？")) {
                    stopScan();
                    isScan = false;
                    window.open("", "_self");
                    top.opener = null;
                    top.close();
                }
            } else {
                window.open("", "_self");
                top.opener = null;
                top.close();
            }
        }

        function validateIpFormate(ipValue) {
            var pattern = "^(\d{1,2}|1\d\d|2[0-3]\d|22[0-3])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$";
            var result = ipValue.match(pattern);
            if (result != null) {
                return true;
            } else {
                return false;
            }
        }

        function stopScan() {
            $.ajax({
                url: '@Url.Action("StopHostScan")',
                cache: false,
                type: 'post',
                dataType: 'json',
                success: function(result) {
                    if (result && result.success) {
                        $("#btnStop").attr("disabled", "disabled");
                        $("#btnScan").removeAttr("disabled");
                        $("#btnScan").val("扫描");
                        isScan = false;
                        return;
                    }
                    alert('停止扫描失败,请重试');
                }
            });
        }

        function beginScan() {
            isScan = true;
            $.ajax({
                type: "POST",
                url: "@Url.Action("PerformsAScan")",
                data: { "startIp": $("#hidStartIp").val(), "endIp": $("#hidEndIp").val() },
                dataType: 'Json',
                success: function(msg) {
                    if (msg.Success) {
                        hostScan("");
                        return;
                    }
                    if (msg.Message) {
                        var resultHtml =
                            "<tr><td class=\"chk_showdialog_td_center\" colspan=\"5\">" + msg.Message + "</td></tr>";
                        $("#table_HostScanResults").append(resultHtml);
                        stopScan();
                    }
                }
            });
        }

        function hostScan(maxHostScanId) {
            if (!isScan) return;
            $.ajax({
                type: "POST",
                url: "@Url.Action("ExcuteHostScan")",
                data: { "maxHostResultId": maxHostScanId },
                dataType: 'Json',
                success: function(msg) {
                    if (!msg.Success && msg.Expandos && (msg.Expandos.timeout || msg.Expandos.neverStart)) {
                        $("#table_HostScanResults").append("<tr><td class=\"chk_showdialog_td_center\" colspan=\"5\">" + msg.Message + "</td></tr>");
                        stopScan();
                        return;
                    }
                    if (msg.Success && msg.Expandos) {
                        if (msg.Expandos.listModels) {
                            var insertHtml = "";
                            $.each(eval(msg.Expandos.listModels), function(i, n) {
                                insertHtml +=
                                    "<tr id=\"" + n.Id + "\"><td class=\"chk_showdialog_td_center\">" +
                                        n.Id + "</td><td class=\"chk_showdialog_td_center\">" + n.Ip +
                                        "</td><td class=\"chk_showdialog_td_center\">" + n.Mac +
                                        "</td><td class=\"chk_showdialog_td_center\">" + n.ActionCode +
                                        "</td><td class=\"chk_showdialog_td_center\">" + n.CreatedTime +
                                        "</td></tr>";
                            });
                            $("#table_HostScanResults").append(insertHtml);
                        }
                        if (msg.Expandos.scanEnd == "no") {
                            setTimeout(function() { hostScan(msg.Expandos.maxId); }, 5000);
                        } else {
                            stopScan();
                        }
                    }
                }
            });
        }
    </script>
}
<div>
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary()
        <div class="editor-label" style="width: 20%" id="divScan">
            @Html.LabelFor(model => model.StartIP)
            @Html.TextBoxFor(model => model.StartIP, new {id = "hidStartIp", placeholder = @ViewResource.Task_HostScan_IpFormate})
            <br />
            @Html.LabelFor(model => model.EndIP)
            @Html.TextBoxFor(model => model.EndIP, new {id = "hidEndIp", placeholder = @ViewResource.Task_HostScan_IpFormate})
        </div>
        <div class="editor-label" style="width: 70%;" id="divInfo">
            <p>说明:扫描前请在主界面【设置向导】里面检查绑定的网卡是否正确</p>
            <div style="color: red;">
                <p>注意:扫描仅是检查局域网内主机是否存在，而检查是否正常与扫描无关</p>
                <p>程序正在扫描中，如果关闭当前窗口，程序将停止扫描</p>
            </div>
            <input id="btnScan" type="button" value="@Resource.Button_Scanning" 
                @((ViewBag.IsLocking != null && ViewBag.IsLocking) ? "disabled='disabled'" : string.Empty) />
            <input id="btnStop" type="button" value="@Resource.Button_Stop" disabled="disabled" />
            <input id="btnExit" type="button" value="@Resource.Button_Exit" />
            @if (ViewBag.IsLocking != null && ViewBag.IsLocking)
            {
                <span style="margin-bottom: 3px; color: red; font-weight: bold;">当前有用户正在操作，请等待该用户结束操作才可进行...</span>
            }
        </div>
        <div class="ui-helper-clearfix"></div>
    }
    
</div>
<div class="divMain">
    <table cellpadding="0" cellspacing="0" class="showdialog_qq_tab">
        <tr>
            <th>序号</th>
            <th>IP地址</th>
            <th>MAC地址</th>
            <th>ActionCode</th>
            <th>添加日期</th>
        </tr>
    </table>
    <div id="tab_content">
        <table id="table_HostScanResults" cellpadding="0" cellspacing="0" class="showdialog_qq_tab" style="border: none;">
        </table>
    </div>
    <div class="ui-helper-clearfix"></div>
</div>

